<template>
  <view v-if="detailList.goods_name" class="scroll-view-container">
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="2000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,i) in detailList.pics" :key='i'>
        <!-- 点击图片实现图片预览 -->
        <image :src="item.pics_big" @click="preview(i)"></image>
      </swiper-item>
    </swiper>

    <!-- 商品的信息区域 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="goods-pic">￥{{detailList.goods_price}}</view>
      <!-- 商品内容 -->
      <view class="goods-item">
        <!-- 商品的名字 -->
        <view class="goods-name">{{detailList.goods_name}}</view>
        <!-- 收藏 -->
        <view class="sc">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text class="sc-text">收藏</text>
        </view>
      </view>
      <!-- 快递运费 -->
      <view class="yf">快递：免运费</view>

    </view>

    <!-- rich-text 富文本属性 商品详情-->
    <rich-text :nodes="detailList.goods_introduce"></rich-text>

    <!-- 购物车 -->
    <view class="shopping">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        detailList: [],
        options: [{
          icon: 'shop',
          text: '店铺',
          infoBackgroundColor: '#007aff',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 2
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    methods: {
      async getGoodsDetail(goods_id) {
        const {
          data: res
        } = await uni.$http.get('/goods/detail', {
          goods_id
        })
        // console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()
        // 处理商品详情中图片空白的问题
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, "<img style='display:block'")
          .replace(/webp/g, 'jpg')
        this.detailList = res.message
      },
      // 图片预览模式
      preview(i) {
        // 调用图片预览API uni.previewImage
        uni.previewImage({
          // 记录预览时的索引
          current: i,
          // 遍历存放图片的数据，把被点击的图片，从数组中获取出来
          urls: this.detailList.pics.map(x => x.pics_big)
        })
      },
      // 点击跳转到购物车页面
      onClick(e){
        // console.log(e)
        if(e.content.text === '购物车'){
          // 页面的切换
          uni.switchTab({
            url:'/pages/cart/cart'
          })
        }
      }
    },
    onLoad(options) {
      const goods_id = options.goods_id
      this.getGoodsDetail(goods_id)
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .goods-info-box {
    padding: 10px;
    padding-right: 0;

    .goods-pic {
      color: #c00000;
      font-size: 18px;
      margin: 10px 0;
    }

    .goods-item {
      display: flex;
      justify-content: space-between;

      .goods-name {
        font-size: 13px;
        padding-right: 10px;
        border-right: 1px solid #efefef;
      }

      .sc {
        width: 120px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        .sc-text {
          color: gray;
          font-size: 12px;
        }
      }
    }

    .yf {
      color: gray;
      font-size: 12px;
      margin: 10px 0;
    }
  }
.shopping{
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
}
  .scroll-view-container {
    padding-bottom: 50px;
  }
</style>
